<template>
  <div class="box-full bgfff box-border round-4 m-relative overflow-hidden">
    <div class="m-absolute t-0 b-0 r-0 l-0 overflow-hidden">
      <a-scrollbar style="height: 100%; width: 100%; overflow-y: auto">
        <div class="p-20">
          <!-- <div class="mb-20">
            <span>测试跟群文案：</span>
          </div> -->
          <a-textarea
            v-model="content"
            placeholder="输入测试文案后点击转链查看效果"
            :auto-size="{
              minRows: 20
            }"
          />
          <div class="w-full h-10"></div>
        </div>
      </a-scrollbar>
    </div>
    <div class="w-full h-16n m-absolute l-0 b-0 r-0 flex flex-center bgfff foote">
      <a-space>
        <a-button @click="content = ''">清除</a-button>
        <a-button type="primary" @click="handleSave">转链</a-button>
      </a-space>
    </div>
  </div>
</template>
<script setup>
import { Message } from '@arco-design/web-vue'
const { ipcRenderer } = window.electron
import axios from 'axios'
const content = ref('')
const userInfo = ref()
;(async function () {
  userInfo.value = await ipcRenderer.invoke('storeGet', 'userInfo')
})()
const handleSave = async () => {
  if (!content.value) {
    Message.error('请输入测试文案')
    return
  }
  let Msg = content.value
  const optionGroupSetting = await window.ipcRenderer.invoke('storeGet', 'optionGroupSetting')
  if (optionGroupSetting?.filterWord?.length) {
    // 如果设置了启用过滤词，并且过滤词数组不为空
    const filterWordsList = optionGroupSetting.filterWord
    if (optionGroupSetting.filterWhole) {
      //设置了有过滤词删除全段，判断当前消息是否包含任意过滤词，如果包含则不再发送
      const index = filterWordsList.findIndex((filterWord) => {
        return Msg.includes(filterWord)
      })
      if (index != -1) resolve(true)
    } else {
      //没有设置有过滤词删除全段，循环过滤词全局替换为空
      filterWordsList.forEach((filterWord) => {
        const escapedDynamicValue = filterWord.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
        const regex = new RegExp(escapedDynamicValue, 'g')
        Msg = Msg.replace(regex, '')
      })
    }
  }
  if (optionGroupSetting?.replaceWord?.length) {
    // 如果设置了关键词替换，循环关键词替换数组并全局替换
    const keyWordsList = optionGroupSetting.replaceWord
    keyWordsList.forEach((item) => {
      const dynamicValue = item.keyword
      const randomIndex = Math.floor(Math.random() * item.words.length) //生成一个随机索引
      const newWord = item.words[randomIndex] //获取随机值
      const escapedDynamicValue = dynamicValue.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
      const regex = new RegExp(escapedDynamicValue, 'g')
      Msg = Msg.replace(regex, newWord)
    })
  }
  // if (optionGroupSetting?.customCopy && isUrl(Msg)) {
  //   // 如果设置了在文案中插入指定文案（这里的文案特指有链接或口令的消息）
  //   if (optionGroupSetting?.customCopy?.position === 'start') {
  //     Msg = `${optionGroupSetting.customCopy.text} ${Msg}`
  //   } else {
  //     Msg = `${Msg} ${optionGroupSetting.customCopy.text}`
  //   }
  // }
  axios
    .post('http://saas-pc-gateway.corpvip.top:8789/turn', {
      openId: userInfo.value.openId,
      content: Msg
    })
    .then(function (response) {
      // 处理成功响应
      content.value = response?.data?.data?.list?.content
      Message.success('转链成功')
    })
    .catch(function (error) {
      // 处理错误
      console.error(error)
      Message.error('转链失败')
    })
}
</script>
<style scoped lang="scss">
.arco-scrollbar {
  height: 100%;
}
.foote {
  border-top: 1px solid #f9f9f9; /* 上部边框 */
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* 上部阴影 */
}
</style>
